﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace SunwellRenderer.Camera
{
	public enum CameraTypes
	{
		GameCamera,
		FreeCamera,
		Fpscamera,


	}
	public class CameraControler
	{
		public Vector3 CameraPosition	= new Vector3(0,10,-50);
		public Vector3 CameraVelocity	= new Vector3(0,10,-50);
		public Vector3 CameraPowerQuack;
		public Vector3 CameraRotation;
		public Vector3 CameraTarget;
		public Vector3 TargetDirection;
		public Vector3 CameraQuack;
		public float CameraMoveAmount = 1;
		public Vector3 CameraDistance;
		public float Turnspeed;
		float cameraRotateAmount = 0.005f;
		float MovePower = 0.5f;
		float RotationPower = 0.03f;
		int lastMouseX,lastMouseY;
		public int CameraMode;
		private Vector2 ScreenSize;
		public Vector3 PositionCameraMax,PositionCameraMin;
		public CameraTypes CameraType = CameraTypes.FreeCamera;
		public CameraControler()
		{
			CameraMode = 1;
			Turnspeed = 10;
			CameraDistance = new Vector3(0,10,65);

			mouseState = Mouse.GetState();
			currentKeyboardState = Keyboard.GetState();
			ScreenSize = new Vector2(1920,1080);
			currentGamePadState = GamePad.GetState(PlayerIndex.One);
		}

		KeyboardState currentKeyboardState;
		GamePadState currentGamePadState;
		MouseState mouseState;
		public void Quack(float x,float y,float z)
		{
			if(x > CameraPowerQuack.X)
			{
				CameraPowerQuack.X = x;
			}
			if(y > CameraPowerQuack.Y)
			{
				CameraPowerQuack.Y = y;
			}
			if(z > CameraPowerQuack.Z)
			{
				CameraPowerQuack.Z = z;
			}
		}
		public void ChangeMode(Vector3 max,Vector3 min,int mode)
		{
			PositionCameraMax.X = max.X;
			PositionCameraMin.X = min.X;
			CameraMode = mode;
		}

		public void Update(GameTime gameTime,ref Matrix worldCam)
		{
			currentKeyboardState = Keyboard.GetState();
			currentGamePadState = GamePad.GetState(PlayerIndex.One);
			mouseState = Mouse.GetState();

			CameraQuack = (CameraPowerQuack * (float)Math.Cos(gameTime.TotalGameTime.TotalSeconds * 50));
			CameraPowerQuack	+= (Vector3.Zero - CameraPowerQuack) / new Vector3(2,5,2);

			if(currentKeyboardState.IsKeyDown(Keys.W))
				CameraVelocity += worldCam.Forward * CameraMoveAmount;
			if(currentKeyboardState.IsKeyDown(Keys.S))
				CameraVelocity += worldCam.Backward * CameraMoveAmount;
			if(currentKeyboardState.IsKeyDown(Keys.A))
				CameraVelocity += worldCam.Left * CameraMoveAmount;
			if(currentKeyboardState.IsKeyDown(Keys.D))
				CameraVelocity += worldCam.Right * CameraMoveAmount;
			if(currentKeyboardState.IsKeyDown(Keys.Q))
				CameraVelocity += worldCam.Up * CameraMoveAmount;
			if(currentKeyboardState.IsKeyDown(Keys.E))
				CameraVelocity += worldCam.Down * CameraMoveAmount;



			if(lastMouseX == -1)
				lastMouseX = mouseState.X;
			if(lastMouseY == -1)
				lastMouseY = mouseState.Y;

			int mouseMoveX = mouseState.X - lastMouseX;
			int mouseMoveY = mouseState.Y - lastMouseY;


			if(mouseState.RightButton == ButtonState.Pressed)
			{
				CameraRotation.X	-= cameraRotateAmount * mouseMoveX;
				CameraRotation.Y	-= cameraRotateAmount * mouseMoveY;

			}

			lastMouseX	= mouseState.X;
			lastMouseY	= mouseState.Y;



			CameraPosition	+= ((CameraVelocity + CameraQuack) - CameraPosition) / 10f;

			worldCam	= Matrix.CreateFromYawPitchRoll(CameraRotation.X,CameraRotation.Y,0);
			worldCam.Translation	= CameraPosition;
		}
	}
}
